package com.hongxun.project.assets.pCablePit.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.hongxun.common.utils.text.Convert;
import com.hongxun.project.assets.iLine.domain.ILine;
import com.hongxun.project.assets.iLine.service.IILineService;
import com.hongxun.project.assets.pPoint.domain.PPoint;
import com.hongxun.project.assets.pPoint.service.IPPointService;
//import com.sun.org.apache.xpath.internal.operations.Mod;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.hongxun.framework.aspectj.lang.annotation.Log;
import com.hongxun.framework.aspectj.lang.enums.BusinessType;
import com.hongxun.project.assets.pCablePit.domain.PCablePit;
import com.hongxun.project.assets.pCablePit.service.IPCablePitService;
import com.hongxun.framework.web.controller.BaseController;
import com.hongxun.framework.web.page.TableDataInfo;
import com.hongxun.framework.web.domain.AjaxResult;
import com.hongxun.common.utils.poi.ExcelUtil;

/**
 * 电缆井采集 信息操作处理
 *
 * @author hongxun
 * @date 2019-03-19
 */
@Controller
@RequestMapping("/assets/pCablePit")
public class PCablePitController extends BaseController {
    private String prefix = "assets/pCablePit";

    @Autowired
    private IPCablePitService pCablePitService;
    @Autowired
    private IPPointService pPointService;
    @Autowired
    private IILineService iLineService;


    @RequiresPermissions("assets:pCablePit:view")
    @GetMapping()
    public String pCablePit() {
        return prefix + "/pCablePit";
    }

	/*@GetMapping("/task_pCablePit/{taskId}")
    public String task_pCablePit(@PathVariable("taskId") String taskId , ModelMap modelMap)
	{
		modelMap.put("taskId",taskId);
	    return prefix + "/task-pCablePit";
	}*/

    /**
     * 查询电缆井采集列表
     */
    @RequiresPermissions("assets:pCablePit:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PCablePit pCablePit) {
        startPage();
        List<PCablePit> list = pCablePitService.selectPCablePitList(pCablePit);
        return getDataTable(list);
    }


    /**
     * 导出电缆井采集列表
     */
    @RequiresPermissions("assets:pCablePit:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PCablePit pCablePit) {
        List<PCablePit> list = pCablePitService.selectPCablePitList(pCablePit);
        ExcelUtil<PCablePit> util = new ExcelUtil<PCablePit>(PCablePit.class);
        return util.exportExcel(list, "pCablePit");
    }

    /**
     * 新增电缆井采集
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存电缆井采集
     */
    @RequiresPermissions("assets:pCablePit:add")
    @Log(title = "电缆井采集", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PCablePit pCablePit) {
        return toAjax(pCablePitService.insertPCablePit(pCablePit));
    }

    /**
     * 修改电缆井采集
     */
    @GetMapping("/edit/{id}/{taskId}")
    public String edit(@PathVariable("id") String id, @PathVariable("taskId") String taskId, ModelMap mmap) {
        PCablePit pCablePit = pCablePitService.selectPCablePitByIdForState(id, taskId);
        PPoint pPoint = pPointService.selectPPointByIdForState(pCablePit.getPId(),taskId);
//        ILine iLine = iLineService.selectILineById(pCablePit.getLineId());
        pCablePit.setPId(pPoint.getCode());
		pCablePit.setLineId(pPoint.getLineId());
		pCablePit.setByqId(pPoint.getByqId());
        mmap.put("pCablePit", pCablePit);
        return prefix + "/edit";
    }

    /**
     * 修改保存电缆井采集
     */
    @RequiresPermissions("assets:pCablePit:edit")
    @Log(title = "电缆井采集", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PCablePit pCablePit) {
        PPoint pPoint = pPointService.selectPPointByIdForState(pCablePit.getPId(),pCablePit.getTaskId());
        pCablePit.setPId(pPoint.getId());
        pCablePit.setByqId(pPoint.getByqId());
        pCablePit.setLineId(pPoint.getLineId());
        pCablePit.setUpdateUser(Integer.parseInt(getUserId().toString()));
		pCablePit.setUpdateDate(new Date());
        pCablePit.setDatastate("修改");
        return toAjax(pCablePitService.updatePCablePit(pCablePit));
    }

    /**
     * 删除电缆井采集
     */
    @RequiresPermissions("assets:pCablePit:remove")
    @Log(title = "电缆井采集", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(pCablePitService.deletePCablePitByIds(ids));
    }


    /**
     * 删除电缆井采集
     */
    @RequiresPermissions("assets:pCablePit:remove")
    @Log(title = "电缆井采集", businessType = BusinessType.DELETE)
    @PostMapping("/remove2Id")
    @ResponseBody
    public AjaxResult remove2Id(String ids , String taskIds) {

        String[] str = Convert.toStrArray(ids);
        int i = 0;
        for (String s : str) {
            PCablePit pCablePit = new PCablePit();
            pCablePit.setId(s);
            pCablePit.setTaskId(taskIds);
            pCablePit.setDatastate("删除");
            i = pCablePitService.updatePCablePit(pCablePit);
        }
        return toAjax(i);
//        return toAjax(pCablePitService.deletePCablePitByIdsAndTaskIds(ids , taskIds));
    }

}
